﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public partial class LayAirTerminalAreaForm : System.Windows.Forms.Form
	{
		public LayAirTerminalAreaForm(HVACUserSetting userSetting, string name)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.initlbText(name);
			this.initXmlboot(name);
		}

		public LayAirTerminalAreaForm(UserSetting userSetting, string name)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.initlbText(name);
			this.initXmlboot(name);
		}

		private void initlbText(string name)
		{
			this.Text = name + "区域布置";
			this.groupBox4.Text = "首个" + name + "与边的距离";
			this.label5.Text = "和" + name + "间距的比值：";
			this.groupBox5.Text = "第一个" + name + "与边的距离";
		}

		private void initXmlboot(string name)
		{
			if (name == "风口")
			{
				this.xmlbootname = "ductlaybatch";
				return;
			}
			if (name == "风盘")
			{
				this.xmlbootname = "ductcoillaybatch";
				return;
			}
			if (name == "器具")
			{
				this.xmlbootname = "PlumbingFixtureslaybatch";
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.hytbRowAngle.Text == "")
			{
				YJKMessageBox.Information("行向角参数不能为空！");
				this.hytbRowAngle.Focus();
				return;
			}
			string limitStr = "1";
			if (this.rdoLimitNumber.Checked)
			{
				this.layAreaParameter.LimitFlag = 1;
				this.layAreaParameter.RowCount = Convert.ToInt32(this.hytbRowCount.Text.Trim());
				this.layAreaParameter.ColCount = Convert.ToInt32(this.hytbColCount.Text.Trim());
				this.layAreaParameter.Ratio = Convert.ToDouble(this.hytbRatio.Text.Trim());
				limitStr = "1";
			}
			else if (this.rdoLimitDistance.Checked)
			{
				this.layAreaParameter.LimitFlag = 2;
				this.layAreaParameter.RowDistance = Convert.ToDouble(this.hytbRowDistance.Text.Trim());
				this.layAreaParameter.ColDistance = Convert.ToDouble(this.hytbColDistance.Text.Trim());
				this.layAreaParameter.RowDistanceSide = Convert.ToDouble(this.hytbRowDistanceSide.Text.Trim());
				this.layAreaParameter.ColDistanceSide = Convert.ToDouble(this.hytbColDistanceSide.Text.Trim());
				limitStr = "2";
			}
			this.LaySettingWrite(limitStr);
			this.GetRectLayPara();
			base.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void LayAirTerminalAreaForm_Load(object sender, EventArgs e)
		{
			this.LaySettingInit();
		}

		private void LaySettingInit()
		{
			this.hytbRowCount.Text = this.userSettingS.GetValue(this.xmlbootname, "rowcount");
			this.hytbColCount.Text = this.userSettingS.GetValue(this.xmlbootname, "colcount");
			this.hytbRatio.Text = this.userSettingS.GetValue(this.xmlbootname, "ratio");
			this.hytbRowDistance.Text = this.userSettingS.GetValue(this.xmlbootname, "rowdistance");
			this.hytbColDistance.Text = this.userSettingS.GetValue(this.xmlbootname, "coldistance");
			this.hytbRowDistanceSide.Text = this.userSettingS.GetValue(this.xmlbootname, "rowsidedistance");
			this.hytbColDistanceSide.Text = this.userSettingS.GetValue(this.xmlbootname, "colsidedistance");
			string value = this.userSettingS.GetValue(this.xmlbootname, "limit");
			this.hytbRowAngle.Text = this.userSettingS.GetValue(this.xmlbootname, "rowAngle");
			if (this.hytbRowAngle.Text == "")
			{
				this.hytbRowAngle.Text = "0";
			}
			if (value == "2")
			{
				this.rdoLimitDistance.Checked = true;
				this.rdoLimitNumber.Checked = false;
				this.layAreaParameter.LimitFlag = 2;
				this.layAreaParameter.RowDistance = Convert.ToDouble(this.hytbRowDistance.Text.Trim());
				this.layAreaParameter.ColDistance = Convert.ToDouble(this.hytbColDistance.Text.Trim());
				this.layAreaParameter.RowDistanceSide = Convert.ToDouble(this.hytbRowDistanceSide.Text.Trim());
				this.layAreaParameter.ColDistanceSide = Convert.ToDouble(this.hytbColDistanceSide.Text.Trim());
				return;
			}
			this.rdoLimitNumber.Checked = true;
			this.rdoLimitDistance.Checked = false;
			this.layAreaParameter.LimitFlag = 1;
			this.layAreaParameter.RowCount = Convert.ToInt32(this.hytbRowCount.Text.Trim());
			this.layAreaParameter.ColCount = Convert.ToInt32(this.hytbColCount.Text.Trim());
			this.layAreaParameter.Ratio = Convert.ToDouble(this.hytbRatio.Text.Trim());
		}

		private void LaySettingWrite(string limitStr)
		{
			this.userSettingS.WriteValue(this.xmlbootname, "rowcount", this.hytbRowCount.Text.Trim());
			this.userSettingS.WriteValue(this.xmlbootname, "colcount", this.hytbColCount.Text.Trim());
			this.userSettingS.WriteValue(this.xmlbootname, "ratio", this.hytbRatio.Text.Trim());
			this.userSettingS.WriteValue(this.xmlbootname, "rowdistance", this.hytbRowDistance.Text.Trim());
			this.userSettingS.WriteValue(this.xmlbootname, "coldistance", this.hytbColDistance.Text.Trim());
			this.userSettingS.WriteValue(this.xmlbootname, "rowsidedistance", this.hytbRowDistanceSide.Text.Trim());
			this.userSettingS.WriteValue(this.xmlbootname, "colsidedistance", this.hytbColDistanceSide.Text.Trim());
			this.userSettingS.WriteValue(this.xmlbootname, "limit", limitStr);
			this.userSettingS.WriteValue(this.xmlbootname, "rowAngle", this.hytbRowAngle.Text.Trim());
		}

		private void rdoLimitNumber_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoLimitNumber.Checked)
			{
				this.rdoLimitDistance.Checked = false;
				this.RdoNumberGroup(true);
				return;
			}
			this.RdoNumberGroup(false);
		}

		private void RdoNumberGroup(bool check)
		{
			this.hytbRowCount.Enabled = check;
			this.hytbColCount.Enabled = check;
			this.hytbRatio.Enabled = check;
		}

		private void RdoDistanceGroup(bool check)
		{
			this.hytbRowDistance.Enabled = check;
			this.hytbColDistance.Enabled = check;
			this.hytbRowDistanceSide.Enabled = check;
			this.hytbColDistanceSide.Enabled = check;
		}

		private void rdoLimitDistance_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoLimitDistance.Checked)
			{
				this.rdoLimitNumber.Checked = false;
				this.RdoDistanceGroup(true);
				return;
			}
			this.RdoDistanceGroup(false);
		}

		private void hytbRowDistance_Validated(object sender, EventArgs e)
		{
			if (sender is YJKTextBox)
			{
				YJKTextBox hytextBox = sender as YJKTextBox;
				double num = double.Parse(hytextBox.Text.Trim());
				if (num < 10.0 || num > 20000.0)
				{
					YJKMessageBox.Information("输入数值超出范围，请重设！");
					hytextBox.Undo();
					hytextBox.Focus();
				}
			}
		}

		private void GetRectLayPara()
		{
			this.rectLayParas = new RectLayParamers();
			this.rectLayParas.LayType = (int)Convert.ToInt16(this.rdoLimitDistance.Checked);
			this.rectLayParas.XSpace = UnitConvert.LengthToStd(Convert.ToDouble(this.hytbRowDistance.Text.Trim()) / 1000.0);
			this.rectLayParas.YSpace = UnitConvert.LengthToStd(Convert.ToDouble(this.hytbColDistance.Text.Trim()) / 1000.0);
			this.rectLayParas.XMargin = UnitConvert.LengthToStd(Convert.ToDouble(this.hytbRowDistanceSide.Text.Trim()) / 1000.0);
			this.rectLayParas.YMargin = UnitConvert.LengthToStd(Convert.ToDouble(this.hytbColDistanceSide.Text.Trim()) / 1000.0);
			this.rectLayParas.RowCount = Convert.ToInt32(this.hytbRowCount.Text.Trim());
			this.rectLayParas.ColCount = Convert.ToInt32(this.hytbColCount.Text.Trim());
			this.rectLayParas.Margin = Convert.ToDouble(this.hytbRatio.Text.Trim());
			this.rectLayParas.AngleRow = Convert.ToDouble(this.hytbRowAngle.Text.Trim());
			this.rectLayParas.LinkLineWay = 0;
			this.rectLayParas.DislocationWay = 0;
		}

		private UserSetting userSettingS = new UserSetting();

		public LayAreaParameter layAreaParameter = new LayAreaParameter();

		public RectLayParamers rectLayParas;

		private string xmlbootname = "ductlaybatch";
	}
}
